package com.guyan.controller;

import com.guyan.entity.User;
import com.guyan.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpSession;

/**
 * ClassName： PageController
 * Date： 2021/6/2 11:13
 *
 * @author 孤焰
 * @version 1.0
 **/

@Controller
public class PageController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ShippingService shippingService;
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    @GetMapping(value = { "/","/index"})
    public String index() {
        return "index";
    }

    @GetMapping("/home/console")
    public String console() {
        return "home/console";
    }

    @GetMapping("/user/loginPage")
    public String loginPage(HttpSession session) {
        session.setAttribute("user", null);
        return "user/login";
    }

    @GetMapping("/user/userPage")
    public String userPage() {
        return "user/page";
    }

    @GetMapping("/user/userAdd")
    public String userAdd() {
        return "user/add";
    }

    @GetMapping("/user/userUpdate")
    public String userUpdate(ModelMap modelMap, Integer id) {
        modelMap.put("userInfo", userService.getUserById(id));
        return "user/update";
    }

    @GetMapping("/product/productPage")
    public String productPage() {
        return "product/page";
    }

    @GetMapping("/product/productAdd")
    public String productAdd(ModelMap modelMap) {
        modelMap.put("categoryList", categoryService.categoryLeafList());
        return "product/add";
    }

    @GetMapping("/product/productUpdate")
    public String productUpdate(ModelMap modelMap, Integer id) {
        modelMap.put("categoryList", categoryService.categoryLeafList());
        modelMap.put("productInfo", productService.getProductById(id));
        return "product/update";
    }

    @GetMapping("/category/categoryPage")
    public String categoryPage() {
        return "category/page";
    }

    @GetMapping("/category/categoryAdd")
    public String categoryAdd() {
        return "category/add";
    }

    @GetMapping("/category/categoryUpdate")
    public String categoryUpdate(ModelMap modelMap, Integer id) {
        modelMap.put("category", categoryService.getCategoryById(id));
        return "category/update";
    }

    @GetMapping("/shipping/shippingPage")
    public String shippingPage() {
        return "shipping/page";
    }

    @GetMapping("/shipping/shippingAdd")
    public String shippingAdd() {
        return "shipping/add";
    }

    @GetMapping("/shipping/shippingUpdate")
    public String shippingUpdate(ModelMap modelMap, Integer id) {
        modelMap.put("shippingInfo", shippingService.getShippingById(id));
        return "shipping/update";
    }

    @GetMapping("/cart/cartPage")
    public String cartPage() {
        return "cart/page";
    }

    @GetMapping("/cart/cartAdd")
    public String cartAdd(ModelMap modelMap) {
        modelMap.put("productList", productService.productListNoPage());
        return "cart/add";
    }

    @GetMapping("/cart/cartUpdate")
    public String cartUpdate(ModelMap modelMap, Integer id) {
        modelMap.put("productList", productService.productListNoPage());
        modelMap.put("cartInfo", cartService.getCartById(id));
        return "cart/update";
    }

    @GetMapping("/order/orderPage")
    public String orderPage() {
        return "order/page";
    }

    @GetMapping("/order/orderAdd")
    public String orderAdd(ModelMap modelMap, HttpSession session) {
        User user = (User) session.getAttribute("user");
        modelMap.put("shippingList", shippingService.shippingListNoPage(user.getId()));
        return "order/add";
    }

    @GetMapping("/order/orderUpdate")
    public String orderUpdate(ModelMap modelMap, HttpSession session, Integer id) {
        User user = (User) session.getAttribute("user");
        modelMap.put("shippingList", shippingService.shippingListNoPage(user.getId()));
        modelMap.put("orderInfo", orderService.getOrderById(id));
        return "order/update";
    }
}
